<?php

/*

	MMetaverse - Open Metaverse Access Library

	Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

	http://www.maryjanesoft.com
	http://www.marlonj.com

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General
	Public License along with this library; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place, Suite 330,
	Boston, MA  02111-1307  USA

	$Id$

*/

/**
 * @package mjs.mmetaverse.manager
 */

/**
 * 
 * Configuration of the library
 */
class MConfig {

    /**
     * Name of the configuration file 
     * @var string 
     */
    private $fileName;

    /**
     * XML with the configuration values 
     *
     * @var SimpleXMLElement
     */
    private $xml;

    /**
     * Create the configuration with a xml element
     *
     * @param string $filename Name of the configuration file
     * @param SimpleXMLElement $xml The xml with the configuration
     */
    private function MConfig($filename, SimpleXMLElement $xml) {
        $this->fileName = $filename;
        $this->xml = $xml;
    }

    /**
     * Return the path where the OpenSim is installed 
     *
     * @return string path where the OpenSim is installed
     */
    public function getOpenSimPath() {
        return (string) $this->xml->opensim->path;
    }

    /**
     * Update the opensim path
     * 
     * @param string $opensimPath 
     */
    public function setOpenSimPath($opensimPath) {
        $this->xml->opensim->path = $opensimPath;
    }

    /**
     * Return the database engine 
     * 
     * @return string Name of the database engine, for example sqlite or mysql 
     */
    public function getDatabaseEngine() {
        // Get database engine 
        return (string) $this->xml->opensim->databaseEngine;
    }

    /**
     * Update the database engine 
     *
     * @param string $databaseEngine
     */
    public function setDatabaseEngine($databaseEngine) {
        // Check if the database is not supported
        if(!$this->databaseSupported($databaseEngine)) {
            throw new Exception('Database Not Supported');
        }

        // Set new database engine 
        $this->xml->opensim->databaseEngine = $databaseEngine;
    }

    /**
     * Load the configuration from a xml file
     *
     * @param string $fileName The path to the xml configuration file
     * @return MOSConfig The current configuration 
     */
    public static function load($fileName) {
         // Check if the file exists
        if(!is_file($fileName))
            throw new Exception("Config file not Found");

        // Load the xml and create a new config object
        $xml = simplexml_load_file($fileName);
        return new MConfig($fileName,$xml);
    }

    /**
     * Verify information
     */
    private function verify() {
        // Verify opensim path
        if(!is_dir($this->getOpenSimPath())) {
            throw new Exception('Invalid OpenSim Path');
        }
    }

    /**
     * Verify database engine name
     */
    private function databaseSupported($databaseEngine) {
        // Supported engines 
        $engines = array('sqlite','mysql');

        // Check if the database name is in the array
        return in_array($databaseEngine, $engines);
    }

    /**
     * Save the configuration inside the xaml file 
     */
    public function save() {
        // Check if the data can be read 
        if(!is_writable($this->fileName)) {
            throw new Exception('Config File Not Writable');
        }

        // Verify information
        $this->verify();
        // Save the new xml 
        $this->xml->asXML($this->fileName);
    }
}

?>
